import axios from "axios"
import {Message} from "element-ui"
import store from '@/store'
import router from '@/router'

const devUrl = 'http://192.168.20.106:8000/api'

const request = axios.create({
    baseURL:devUrl,
    timeout:5000
})

// 请求拦截器
request.interceptors.request.use(aaa => {
    // 每次都将 token传递给后端服务器了
    // 设置动态token
    let token = store.state.user.user.token
    aaa.headers['token'] = token
    return aaa//拦截了，这里要原封不动的返回
}, err => {
    console.log(err, '请求失败');
    return err
})

// 响应拦截器
request.interceptors.response.use(res => {
    // console.log(res, '++++++++++++');
    if(res.data.code==10119){
        // 清空token
        store.commit("user/SET_USER",{})
        //刷新路由
        router.history.go(0)
    }
    if(res.data.code!==200){
        Message({
            type:"success",
            message:res.data.message
        })
    }
    return res.data
}, err => {
    console.log(err, '响应失败');
    return err
})


export default request